package com.ptszyxx.popose.module.base.login.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YLoginUtil;
import com.ptszyxx.popose.common.utils.YWhiteIpUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.login.LoginResult;
import com.ysg.utils.YMD5Utils;
import com.ysg.utils.YPhoneUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class LoginVM extends BaseViewModel<CommonRepository> {
//    public ObservableField<String> phone = new ObservableField<>("10107163011");//三月的小雨
//    public ObservableField<String> phone = new ObservableField<>("15773490302");//爱看上面的豆豆
//    public ObservableField<String> phone = new ObservableField<>("13950104779");
//    public ObservableField<String> phone = new ObservableField<>("18850903537");
//    public ObservableField<String> phone = new ObservableField<>("101101");
//    public ObservableField<String> password = new ObservableField<>("123456");

    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<Boolean> privacyChecked = new ObservableField<>(false);

    public LoginVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.login);
    }

    public BindingCommand onLoginCommand = new BindingCommand(()->requestLogin());
    public BindingCommand onSmsCodeCommand = new BindingCommand(()->onJumpLoginCode());
    public BindingCommand onForgetPasswordCommand = new BindingCommand(()->YActivityUtil.getInstance().jumpPasswordForget(this));
    public BindingCommand onAgreementCommand = new BindingCommand(()->jumpAgreement());
    public BindingCommand onPrivacyCommand = new BindingCommand(()->jumpPrivacy());
    public BindingCommand onTestCommand = new BindingCommand(()->YActivityUtil.getInstance().jumpTest(this));

    /**
     * 登录
     */
    private void requestLogin(){
        if (!checkLogin()) return;

        Map<String, String> map = new HashMap<>();
        map.put("loginname", phone.get());
        map.put("password", YMD5Utils.get(password.get()));
        map.put("loginType", "1");
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("oaid", YSPUtils.getInstance().getOAID());

        HttpUtils.getInstance()
                .data(model.login(map), this,
                        new OnSuccessResult<LoginResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<LoginResult> response) {
                                requestIsWhiteIp();
                                YLoginUtil.getInstance().login(LoginVM.this, response.getData());
                            }
                        });
    }

    /**
     * 白名单ip
     */
    public void requestIsWhiteIp() {
        YWhiteIpUtil.getInstance().requestIsWhiteIp(this, model);
    }

    /**
     * 登录前校验
     */
    private boolean checkLogin(){
        if (YStringUtil.isEmpty(phone.get())){
            YToastUtil.showShort(R.string.et_phone_hint);
            return false;
        }
        if (YStringUtil.isEmpty(password.get())){
            YToastUtil.showShort(R.string.et_password_hint);
            return false;
        }
        if (!privacyChecked.get()){
            YToastUtil.showShort(R.string.login_privacy_check_tip);
            return false;
        }
        return true;
    }

    public void jumpAgreement() {
        YActivityUtil.getInstance().jumpAgreement(this);
    }
    public void jumpPrivacy() {
        YActivityUtil.getInstance().jumpPrivacy(this);
    }

    private void onJumpLoginCode() {
        YActivityUtil.getInstance().jumpLoginCode(this);
        YActivityUtil.getInstance().close(this);
    }




}
